package com.wedo.mall.activity.agent;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.utils.EViewUtils;
import com.e.library.widget.ETitleBar;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wedo.mall.R;
import com.wedo.mall.WApp;
import com.wedo.mall.activity.main.SearchActivity;
import com.wedo.mall.adapter.AgentAdapter;
import com.wedo.mall.entity.Agent;
import com.wedo.mall.entity.AgentCategory;
import com.wedo.mall.ptr.Pull2Refresh;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 商家推荐或不同分类商家列表UI
 * 1、首页店铺推荐-店铺推荐
 * 2、商家-商家分类-商家列表
 * Created by e on 2017/7/15.
 */
public class AgentsActivity extends EActivity {

    public static final String EXTRA_URL = "url";
    public static final String EXTRA_TITLE = "title";
    public static final String EXTRA_CATEGORY = "category";


    public static void toRecommend(EActivity activity){
        String title = activity.getString(R.string.index_recommend_agent);
        toAgentsActivity(activity,EApi.AGENT_RECOMMEND,title,null);
    }

    public static void toAgents(EActivity activity,AgentCategory category){
        toAgentsActivity(activity,EApi.AGENT_LIST,category.category_name,category);
    }

    public static void toAgentsActivity(EActivity activity,String url,String title,AgentCategory category){
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_URL,url);
        bundle.putString(EXTRA_TITLE,title);
        bundle.putParcelable(EXTRA_CATEGORY,category);
        activity.toActivity(AgentsActivity.class,bundle);
    }


    @BindView(R.id.title_bar) ETitleBar title_bar;
    @BindView(R.id.tv_empty) TextView tv_empty;
    @BindView(R.id.rv_list) XRecyclerView rv_list;

    private Pull2Refresh<Agent> ptr;

    private SparseArray<String> sorts = new SparseArray<>(2);

    @Override
    protected int layout() {
        return R.layout.activity_agents;
    }

    @Override
    protected void onActivityCreated(Bundle state) {

        Intent intent = getIntent();

        String url = intent.getStringExtra(EXTRA_URL);
        String title = intent.getStringExtra(EXTRA_TITLE);
        AgentCategory category = intent.getParcelableExtra(EXTRA_CATEGORY);

        title_bar.setTitle(title);

        rv_list.setHasFixedSize(true);
        EViewUtils.vertical(rv_list);

        ptr = new Pull2Refresh<>();
        AgentAdapter adapter = AgentAdapter.search(this,null);
        adapter.listener(this);

        Map<String,String> params = WApp.lngLat();
        if (category !=null){
            //Map<String,String> params = WApp.lngLat();
            params.put("category_id",category.id);

            title_bar.hideForwardLayout();

        }else {
            title_bar.showForwardIv();
            title_bar.showForwardLayout();
            title_bar.setOnForwardListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SearchActivity.toSearchAgent(AgentsActivity.this);
                }
            });
        }

        ptr.params(params);
        ptr.widget(rv_list).adapter(adapter).url(url).clazz(Agent.class).emptyWidget(tv_empty).done();

        sorts.put(R.id.tv_sort_hot,"is_hot");
        sorts.put(R.id.tv_sort_nearby,"is_nearby");
    }

    @OnClick({R.id.tv_sort_hot,R.id.tv_sort_nearby})
    protected void onSortClick(View view){
        int id = view.getId();
        String key = sorts.get(id);
        view.setSelected(!view.isSelected());
        /**0表示升序，1表示降序*/
//        String value = view.isSelected()?"0":"1";
//        ptr.param(key,value).refresh();

        if(view.isSelected()) {
            ptr.param(key,"1").refresh();
        }else {
            ptr.removeParam(key).refresh();
        }
    }

}
